package com.stu.manager.organization.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stu.manager.config.Result;
import com.stu.manager.organization.entity.SysDepartment;
import com.stu.manager.organization.entity.dto.AddDepartmentDto;
import com.stu.manager.organization.mapper.SysDepartmentMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-02-12
 */
@Service
public class SysDepartmentServiceImpl extends ServiceImpl<SysDepartmentMapper, SysDepartment> {

    @Resource
    private SysDepartmentMapper departmentMapper;

    //模糊+分页查询学院
    public IPage<SysDepartment> pageDepartment(Page<SysDepartment> pageDepartment, QueryWrapper<SysDepartment> wrapper) {
        IPage<SysDepartment> sysDepartmentIPage = baseMapper.selectPage(pageDepartment, wrapper);
        return sysDepartmentIPage;
    }
    //新增学院
    public Result addDepartment(AddDepartmentDto addDepartmentDto) {
        String departmentCode = addDepartmentDto.getDepartmentCode();
        QueryWrapper<SysDepartment> wrapper = new QueryWrapper<>();
        wrapper.eq("department_code", departmentCode);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0) {
            return Result.error().message("当前学院已存在");
        }
        SysDepartment sysDepartment = new SysDepartment();
        BeanUtils.copyProperties(addDepartmentDto, sysDepartment);
        int insert = baseMapper.insert(sysDepartment);
        if (insert > 0) {
            return Result.ok().message("添加成功");
        } else {
            return Result.error().message("添加失败");
        }
    }
    public SysDepartment getDepartmentByCode(String departmentCode) {
        QueryWrapper<SysDepartment> wrapper = new QueryWrapper<>();
        wrapper.eq("department_code", departmentCode);
        SysDepartment sysDepartment = baseMapper.selectOne(wrapper);
        return sysDepartment;
    }

    public Result updateDepartment(SysDepartment sysDepartment) {
        QueryWrapper<SysDepartment> wrapper = new QueryWrapper<>();
        wrapper.eq("department_code", sysDepartment.getDepartmentCode()).ne("id", sysDepartment.getId());
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0) {
            return Result.error().message("学院代码重复，请修改");
        }
        int insert = baseMapper.updateById(sysDepartment);
        if (insert > 0) {
            return Result.ok().message("修改成功");
        } else {
            return Result.error().message("修改失败");
        }
    }

    public List<SysDepartment> findAllDepartment() {
        List<SysDepartment> sysDepartments = baseMapper.selectList(null);
        return sysDepartments;
    }
}
